package com.link2room.ironhide.controller.manager;

import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.manager.HotelsManagerParamDto;
import com.link2room.ironhide.dto.param.manager.L2RGroupAreaManagerParamDto;
import com.link2room.ironhide.dto.param.manager.L2RServicesParamDto;
import com.link2room.ironhide.dto.result.base.PositionBaseDto;
import com.link2room.ironhide.dto.result.manager.HotelInfoDto;
import com.link2room.ironhide.dto.result.manager.L2RServiceDto;
import com.link2room.ironhide.facade.IHotelManagerFacadeService;
import com.link2room.ironhide.facade.IL2RPositionsFacadeService;
import com.link2room.ironhide.facade.IL2RServicesFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.link2room.ironhide.constant.PermissionConstant.*;

/**
 * Created by sss on 2016/11/1.
 */
@Api(tags = "酒店基础信息管理")
@RestController
@RequestMapping("/manager/HotelBaseInfos")
public class HotelBaseInfoManagerController {
    @Resource
    private IHotelManagerFacadeService hotelManagerFacadeService;

    @Resource
    private IL2RPositionsFacadeService l2RPositionsFacadeServiceImpl;

    @Resource
    private IL2RServicesFacadeService l2RServicesFacadeService;

    @ApiOperation(value = "获取酒店列表", response = HotelInfoDto.class)
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = hotelBaseInfoList, name = "获取酒店列表")
    public BaseResultDto hotelBaseInfoList(
            @RequestParam(value = "lhotelCode", required = false) String lhotelCode,
            @RequestParam(value = "city", required = false) Long city,
            @RequestParam(value = "brands", required = false) String brands,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "currentPage", required = false) Integer currentPage,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "key", required = false) String key
    ) throws Exception {
        HotelsManagerParamDto hotelsManagerParamDto = new HotelsManagerParamDto();
        hotelsManagerParamDto.setLhotelCode(lhotelCode);
        hotelsManagerParamDto.setCity(city);
        hotelsManagerParamDto.setStatus(status);
        hotelsManagerParamDto.setKey(key);
        hotelsManagerParamDto.setCurrentPage(currentPage);
        hotelsManagerParamDto.setPageSize(pageSize);
        if (StringUtil.isNotBlank(brands)) {
            List<String> brandList = new ArrayList();
            String[] brandArr = brands.split(",");
            for (String brand : brandArr) {
                brandList.add(brand);
            }
            hotelsManagerParamDto.setBrands(brandList);
        }
        QueryResult<HotelInfoDto> hotelBaseInfoResult = hotelManagerFacadeService.getHotelBaseInfoList(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(hotelBaseInfoResult);
        return l2rRs;
    }

    @ApiOperation(value = "保存酒店基础信息")
    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = newHotelBaseInfo, name = "新建酒店基础信息详情")
    public BaseResultDto newHotelBaseInfo(
            @RequestBody HotelsManagerParamDto hotelsManagerParamDto
    ) throws Exception {
        hotelManagerFacadeService.saveOrUpdateHotelBaseInfoDetail(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value = "更新酒店基础信息")
    @RequestMapping(value = "/HotelBases",method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateHotelBaseInfo, name = "更新酒店基础信息")
    public BaseResultDto updateHotelBaseInfo(
            @RequestBody HotelsManagerParamDto hotelsManagerParamDto
    ) throws Exception {
        hotelManagerFacadeService.saveOrUpdateHotelBaseInfo(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value = "更新酒店基础信息明细")
    @RequestMapping(method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateHotelBaseInfoDetail, name = "更新酒店基础信息详情")
    public BaseResultDto updateHotelBaseInfoDetail(
            @RequestBody HotelsManagerParamDto hotelsManagerParamDto
    ) throws Exception {
        hotelManagerFacadeService.saveOrUpdateHotelBaseInfoDetail(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value = "获取酒店代码")
    @RequestMapping(value = "/HotelCode", method = RequestMethod.GET)
    @L2RPermissionResources(value = hotelCode, name = "获取酒店代码")
    public BaseResultDto mcCode(
            @RequestParam(value = "lhotelCode", required = true) String lhotelCode
    ) throws Exception {
        HotelsManagerParamDto hotelsManagerParamDto = new HotelsManagerParamDto();
        hotelsManagerParamDto.setLhotelCode(lhotelCode);
        hotelManagerFacadeService.checkHotelCode(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value="获取酒店属性列表",response = PositionBaseDto.class)
    @RequestMapping(value = "/HotelPositions",method = RequestMethod.GET)
    @L2RPermissionResources(value = hotelPositions,name="获取酒店属性列表")
    public BaseResultDto hotelPositions(
            @RequestParam(value = "lhotelCode", required = false) String lhotelCode,
            @RequestParam(value = "cityCode", required = true) Long cityCode
    ) throws Exception {
        L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto = new L2RGroupAreaManagerParamDto();
        l2RGroupAreaManagerParamDto.setLhotelCode(lhotelCode);
        l2RGroupAreaManagerParamDto.setCityCode(cityCode);
        List<PositionBaseDto> l2RGroupPositionBaseList = l2RPositionsFacadeServiceImpl.listPositionBaseHotelUse(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RGroupPositionBaseList);
        return l2rRs;
    }

    @ApiOperation(value = "保存酒店属性")
    @RequestMapping(value = "/HotelPositions", method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateHotelPositions, name = "保存酒店属性")
    public BaseResultDto hotelPositions(
            @RequestBody HotelsManagerParamDto hotelsManagerParamDto
    ) throws Exception {
        l2RPositionsFacadeServiceImpl.saveOrUpdateL2RHotelPosition(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value = "获取酒店服务", response = L2RServiceDto.class)
    @RequestMapping(value = "/HotelServices", method = RequestMethod.GET)
    @L2RPermissionResources(value = listL2RHotelServicesUse, name = "获取酒店服务")
    public BaseResultDto hotelServices(
            @RequestParam(value = "lhotelCode", required = true) String lhotelCode
    ) throws Exception {
        BaseParamDto baseParamDto = new BaseParamDto();
        baseParamDto.setLhotelCode(lhotelCode);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RServicesFacadeService.listL2RHotelServicesUse(baseParamDto));
        return l2rRs;
    }

    @ApiOperation(value="保存酒店服务")
    @RequestMapping(value = "/HotelServices", method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateHotelServices, name="保存酒店服务")
    public BaseResultDto hotelServices(
            @RequestBody L2RServicesParamDto l2RServicesParamDto
    ) throws Exception {
        l2RServicesFacadeService.saveOrUpdateL2RHotelServices(l2RServicesParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value="获取酒店基础信息",response = HotelInfoDto.class)
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @L2RPermissionResources(value = hotelBaseInfo, name="获取酒店基础信息")
    public BaseResultDto hotelBaseInfo(
            @PathVariable Long id
    ) throws Exception {
        HotelsManagerParamDto hotelsManagerParamDto = new HotelsManagerParamDto();
        hotelsManagerParamDto.setId(id);
        HotelInfoDto hotelBaseInfo = hotelManagerFacadeService.getHotelBaseInfo(hotelsManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(hotelBaseInfo);
        return l2rRs;
    }


}
